/*
 * main.c
 *
 *  Created on: 2022��12��5��
 *      Author: deng
 */

#include "DSP2833x_Device.h"
#include "DSP2833x_Examples.h"

/***********************************************************/
#define LED_OFF  	GpioDataRegs.GPCSET.bit.GPIO87 = 1
#define LED_ON   	GpioDataRegs.GPCCLEAR.bit.GPIO87 = 1
#define LED_TOGGLE 	GpioDataRegs.GPCTOGGLE.bit.GPIO87 = 1
void LED_Config(void){
	EALLOW;
	SysCtrlRegs.PCLKCR3.bit.GPIOINENCLK = 1;

	GpioCtrlRegs.GPCMUX2.bit.GPIO87 = 0;
	GpioCtrlRegs.GPCDIR.bit.GPIO87 = 1;
	GpioCtrlRegs.GPCPUD.bit.GPIO87 = 0;
	EDIS;
	LED_OFF;
}
/***********************************************************/


/***********************************************************/
#define U2_11_H	GpioDataRegs.GPASET.bit.GPIO4 	= 1
#define U2_11_L	GpioDataRegs.GPACLEAR.bit.GPIO4 = 1

#define U2_12_H	GpioDataRegs.GPASET.bit.GPIO5 	= 1
#define U2_12_L	GpioDataRegs.GPACLEAR.bit.GPIO5 = 1

#define U2_13_H	GpioDataRegs.GPASET.bit.GPIO6 	= 1
#define U2_13_L	GpioDataRegs.GPACLEAR.bit.GPIO6 = 1

#define U2_16_H	GpioDataRegs.GPASET.bit.GPIO7 	= 1
#define U2_16_L	GpioDataRegs.GPACLEAR.bit.GPIO7 = 1

#define U2_17_H	GpioDataRegs.GPASET.bit.GPIO8 	= 1
#define U2_17_L	GpioDataRegs.GPACLEAR.bit.GPIO8 = 1

#define U2_18_H	GpioDataRegs.GPASET.bit.GPIO9 	= 1
#define U2_18_L	GpioDataRegs.GPACLEAR.bit.GPIO9 = 1

#define U2_19_H	GpioDataRegs.GPASET.bit.GPIO10 	= 1
#define U2_19_L	GpioDataRegs.GPACLEAR.bit.GPIO10 = 1

#define U2_20_H	GpioDataRegs.GPASET.bit.GPIO11 	= 1
#define U2_20_L	GpioDataRegs.GPACLEAR.bit.GPIO11 = 1

#define J5_EN  GpioDataRegs.GPCSET.bit.GPIO85 = 1
#define J5_DIS GpioDataRegs.GPCCLEAR.bit.GPIO85 = 1

#define J6_EN  GpioDataRegs.GPCSET.bit.GPIO84 = 1
#define J6_DIS GpioDataRegs.GPCCLEAR.bit.GPIO84 = 1

#define J7_EN  GpioDataRegs.GPCSET.bit.GPIO86 = 1
#define J7_DIS GpioDataRegs.GPCCLEAR.bit.GPIO86 = 1

void Y_GPIO_Config(void){
	EALLOW;
	SysCtrlRegs.PCLKCR3.bit.GPIOINENCLK = 1;

	//U2-11
	GpioCtrlRegs.GPAMUX1.bit.GPIO4 = 0;
	GpioCtrlRegs.GPADIR.bit.GPIO4 = 1;
	GpioCtrlRegs.GPAPUD.bit.GPIO4 = 0;

	//U2-12
	GpioCtrlRegs.GPAMUX1.bit.GPIO5 = 0;
	GpioCtrlRegs.GPADIR.bit.GPIO5 = 1;
	GpioCtrlRegs.GPAPUD.bit.GPIO5 = 0;

	//U2-13
	GpioCtrlRegs.GPAMUX1.bit.GPIO6 = 0;
	GpioCtrlRegs.GPADIR.bit.GPIO6 = 1;
	GpioCtrlRegs.GPAPUD.bit.GPIO6 = 0;

	//U2-16
	GpioCtrlRegs.GPAMUX1.bit.GPIO7 = 0;
	GpioCtrlRegs.GPADIR.bit.GPIO7 = 1;
	GpioCtrlRegs.GPAPUD.bit.GPIO7 = 0;

	//U2-17
	GpioCtrlRegs.GPAMUX1.bit.GPIO8 = 0;
	GpioCtrlRegs.GPADIR.bit.GPIO8 = 1;
	GpioCtrlRegs.GPAPUD.bit.GPIO8 = 0;

	//U2-18
	GpioCtrlRegs.GPAMUX1.bit.GPIO9 = 0;
	GpioCtrlRegs.GPADIR.bit.GPIO9 = 1;
	GpioCtrlRegs.GPAPUD.bit.GPIO9 = 0;

	//U2-19
	GpioCtrlRegs.GPAMUX1.bit.GPIO10 = 0;
	GpioCtrlRegs.GPADIR.bit.GPIO10 = 1;
	GpioCtrlRegs.GPAPUD.bit.GPIO10 = 0;

	//U2-20
	GpioCtrlRegs.GPAMUX1.bit.GPIO11 = 0;
	GpioCtrlRegs.GPADIR.bit.GPIO11 = 1;
	GpioCtrlRegs.GPAPUD.bit.GPIO11 = 0;

	//J6
	GpioCtrlRegs.GPCMUX2.bit.GPIO84 = 0;
	GpioCtrlRegs.GPCDIR.bit.GPIO84 = 1;
	GpioCtrlRegs.GPCPUD.bit.GPIO84 = 0;

	//J5
	GpioCtrlRegs.GPCMUX2.bit.GPIO85 = 0;
	GpioCtrlRegs.GPCDIR.bit.GPIO85 = 1;
	GpioCtrlRegs.GPCPUD.bit.GPIO85 = 0;

	//J7
	GpioCtrlRegs.GPCMUX2.bit.GPIO86 = 0;
	GpioCtrlRegs.GPCDIR.bit.GPIO86 = 1;
	GpioCtrlRegs.GPCPUD.bit.GPIO86 = 0;


	EDIS;
}

enum{
	J5 = 0,
	J6,
	J7
};

int getBit(char c,int i){
	return (c>>i)&i;
}
Uint16 I ;
void Y_GPIO_OutPut(Uint8 Id,Uint8 Command){//ID 0:J5 1:J6 2 J7

	switch(Id)
	{
		case 0x00:J5_EN;J6_DIS;J7_DIS;break;
		case 0x01:J5_DIS;J6_EN;J7_DIS;break;
		case 0x02:J5_DIS;J6_EN;J7_DIS;break;
	}
	if(getBit(Command,0)) U2_11_H;
	else 				  U2_11_L;
	if(getBit(Command,1)) U2_12_H;
	else 				  U2_12_L;
	if(getBit(Command,2)) U2_13_H;
	else 				  U2_13_L;
	if(getBit(Command,3)) U2_16_H;
	else 				  U2_16_L;
	if(getBit(Command,4)) U2_17_H;
	else 				  U2_17_L;
	if(getBit(Command,5)) U2_18_H;
	else 				  U2_18_L;
	if(getBit(Command,6)) U2_19_H;
	else 				  U2_19_L;
	if(getBit(Command,7)) U2_20_H;
	else 				  U2_20_L;
}

/***********************************************************/


/*************************************************************/

#define O1 GpioDataRegs.GPCDAT.bit.GPIO83
#define O2 GpioDataRegs.GPADAT.bit.GPIO12
#define O3 GpioDataRegs.GPADAT.bit.GPIO13
#define O4 GpioDataRegs.GPADAT.bit.GPIO14
#define O5 GpioDataRegs.GPADAT.bit.GPIO29
#define O6 GpioDataRegs.GPADAT.bit.GPIO15
#define O7 GpioDataRegs.GPADAT.bit.GPIO16

enum{
	OGPIO_PRESS = 0,
	OGPIO_NOPRESS,
};


#define O1_Press 0x01
#define O2_Press 0x02
#define O3_Press 0x03
#define O4_Press 0x04
#define O5_Press 0x05
#define O6_Press 0x06
#define O7_Press 0x07


void O_GPIO_Config(void){
	EALLOW;
	SysCtrlRegs.PCLKCR3.bit.GPIOINENCLK = 1;

	GpioCtrlRegs.GPCMUX2.bit.GPIO83 = 0;
	GpioCtrlRegs.GPCDIR.bit.GPIO83 = 0;
	GpioCtrlRegs.GPCPUD.bit.GPIO83 = 0;

	GpioCtrlRegs.GPAMUX1.bit.GPIO12 = 0;
	GpioCtrlRegs.GPADIR.bit.GPIO12 = 0;
	GpioCtrlRegs.GPAPUD.bit.GPIO12 = 0;

	GpioCtrlRegs.GPAMUX1.bit.GPIO13 = 0;
	GpioCtrlRegs.GPADIR.bit.GPIO13 = 0;
	GpioCtrlRegs.GPAPUD.bit.GPIO13 = 0;

	GpioCtrlRegs.GPAMUX1.bit.GPIO14 = 0;
	GpioCtrlRegs.GPADIR.bit.GPIO14 = 0;
	GpioCtrlRegs.GPAPUD.bit.GPIO14 = 0;

	GpioCtrlRegs.GPAMUX1.bit.GPIO15 = 0;
	GpioCtrlRegs.GPADIR.bit.GPIO15 = 0;
	GpioCtrlRegs.GPAPUD.bit.GPIO15 = 0;

	GpioCtrlRegs.GPAMUX2.bit.GPIO16 = 0;
	GpioCtrlRegs.GPADIR.bit.GPIO16 = 0;
	GpioCtrlRegs.GPAPUD.bit.GPIO16 = 0;

	GpioCtrlRegs.GPAMUX2.bit.GPIO29 = 0;
	GpioCtrlRegs.GPADIR.bit.GPIO29 = 0;
	GpioCtrlRegs.GPAPUD.bit.GPIO29 = 0;
	EDIS;
}

Uint8 Read_O_GPIO(void){
	if(OGPIO_PRESS == O1 || OGPIO_PRESS == O2 || OGPIO_PRESS == O3 ||OGPIO_PRESS == O4 ||OGPIO_PRESS == O5 ||OGPIO_PRESS == O6 ||OGPIO_PRESS == O7 ){
		if(OGPIO_PRESS == O1)return O1_Press;
		if(OGPIO_PRESS == O2)return O2_Press;
		if(OGPIO_PRESS == O3)return O3_Press;
		if(OGPIO_PRESS == O4)return O4_Press;
		if(OGPIO_PRESS == O5)return O5_Press;
		if(OGPIO_PRESS == O6)return O6_Press;
		if(OGPIO_PRESS == O7)return O7_Press;
	}
	 return 0xff;
}

/************************************************************/

/*******************************************************************/
enum{
	XMGPIO_PRESS = 0,
	XMGPIO_NOPRESS,
};
#define XM1 GpioDataRegs.GPADAT.bit.GPIO24
#define XM2 GpioDataRegs.GPADAT.bit.GPIO25
#define XM3 GpioDataRegs.GPBDAT.bit.GPIO57
#define XM4 GpioDataRegs.GPBDAT.bit.GPIO58
#define XM5 GpioDataRegs.GPBDAT.bit.GPIO59

#define XM1_Press 0x01
#define XM2_Press 0x02
#define XM3_Press 0x03
#define XM4_Press 0x04
#define XM5_Press 0x05


void XM_GPIO_Config(void){
	EALLOW;
	SysCtrlRegs.PCLKCR3.bit.GPIOINENCLK = 1;

	GpioCtrlRegs.GPAMUX2.bit.GPIO24 = 0;
	GpioCtrlRegs.GPADIR.bit.GPIO24 = 0;
	GpioCtrlRegs.GPAPUD.bit.GPIO24 = 0;

	GpioCtrlRegs.GPAMUX2.bit.GPIO25 = 0;
	GpioCtrlRegs.GPADIR.bit.GPIO25 = 0;
	GpioCtrlRegs.GPAPUD.bit.GPIO25 = 0;

	GpioCtrlRegs.GPBMUX2.bit.GPIO57 = 0;
	GpioCtrlRegs.GPBDIR.bit.GPIO57 = 0;
	GpioCtrlRegs.GPBPUD.bit.GPIO57 = 0;

	GpioCtrlRegs.GPBMUX2.bit.GPIO58 = 0;
	GpioCtrlRegs.GPBDIR.bit.GPIO58 = 0;
	GpioCtrlRegs.GPBPUD.bit.GPIO58 = 0;


	GpioCtrlRegs.GPBMUX2.bit.GPIO59 = 0;
	GpioCtrlRegs.GPBDIR.bit.GPIO59 = 0;
	GpioCtrlRegs.GPBPUD.bit.GPIO59 = 0;

	EDIS;
}
Uint8 Read_XM_GPIO(void){
	if(XMGPIO_PRESS == XM1 || XMGPIO_PRESS == XM2 || XMGPIO_PRESS == XM3 ||XMGPIO_PRESS == XM4 ||XMGPIO_PRESS == XM5  ){
		if(XMGPIO_PRESS == XM1)return XM1_Press;
		if(XMGPIO_PRESS == XM2)return XM2_Press;
		if(XMGPIO_PRESS == XM3)return XM3_Press;
		if(XMGPIO_PRESS == XM4)return XM4_Press;
		if(XMGPIO_PRESS == XM5)return XM5_Press;
	}
	 return 0xff;
}

/*******************************************************************/
#define J9_0_Press 0x10
#define J9_1_Press 0x11
#define J9_2_Press 0x12
#define J9_3_Press 0x13
#define J9_4_Press 0x14
#define J9_5_Press 0x15
#define J9_6_Press 0x16
#define J9_7_Press 0x17

#define J8_0_Press 0x20
#define J8_1_Press 0x21
#define J8_2_Press 0x22
#define J8_3_Press 0x23
#define J8_4_Press 0x24
#define J8_5_Press 0x25
#define J8_6_Press 0x26
#define J8_7_Press 0x27

#define J11_0_Press 0x30
#define J11_1_Press 0x31
#define J11_2_Press 0x32
#define J11_3_Press 0x33
#define J11_4_Press 0x34
#define J11_5_Press 0x35
#define J11_6_Press 0x36
#define J11_7_Press 0x37

#define U2_89 GpioDataRegs.GPBDAT.bit.GPIO49
#define U2_90 GpioDataRegs.GPBDAT.bit.GPIO50
#define U2_91 GpioDataRegs.GPBDAT.bit.GPIO51
#define U2_94 GpioDataRegs.GPBDAT.bit.GPIO52
#define U2_95 GpioDataRegs.GPBDAT.bit.GPIO53
#define U2_96 GpioDataRegs.GPBDAT.bit.GPIO54
#define U2_97 GpioDataRegs.GPBDAT.bit.GPIO55
#define U2_98 GpioDataRegs.GPBDAT.bit.GPIO56

#define J9_EN 	GpioDataRegs.GPACLEAR.bit.GPIO26 = 1
#define J9_DIS 	GpioDataRegs.GPASET.bit.GPIO26 = 1

#define J8_EN 	GpioDataRegs.GPACLEAR.bit.GPIO27 = 1
#define J8_DIS 	GpioDataRegs.GPASET.bit.GPIO27 = 1

#define J11_EN 	GpioDataRegs.GPBCLEAR.bit.GPIO48 = 1
#define J11_DIS GpioDataRegs.GPBSET.bit.GPIO48 = 1

enum{
	XGPIO_PRESS = 0,
	XGPIO_NOPRESS,
};
enum{
	J8 = 0,
	J9,
	J11,
};


void X_GPIO_Config(void){
	EALLOW;
	SysCtrlRegs.PCLKCR3.bit.GPIOINENCLK = 1;

	GpioCtrlRegs.GPBMUX2.bit.GPIO49 = 0;
	GpioCtrlRegs.GPBDIR.bit.GPIO49 = 0;
	GpioCtrlRegs.GPBPUD.bit.GPIO49 = 0;

	GpioCtrlRegs.GPBMUX2.bit.GPIO50 = 0;
	GpioCtrlRegs.GPBDIR.bit.GPIO50 = 0;
	GpioCtrlRegs.GPBPUD.bit.GPIO50 = 0;

	GpioCtrlRegs.GPBMUX2.bit.GPIO51 = 0;
	GpioCtrlRegs.GPBDIR.bit.GPIO51 = 0;
	GpioCtrlRegs.GPBPUD.bit.GPIO51 = 0;

	GpioCtrlRegs.GPBMUX2.bit.GPIO52 = 0;
	GpioCtrlRegs.GPBDIR.bit.GPIO52 = 0;
	GpioCtrlRegs.GPBPUD.bit.GPIO52 = 0;

	GpioCtrlRegs.GPBMUX2.bit.GPIO53 = 0;
	GpioCtrlRegs.GPBDIR.bit.GPIO53 = 0;
	GpioCtrlRegs.GPBPUD.bit.GPIO53 = 0;

	GpioCtrlRegs.GPBMUX2.bit.GPIO54 = 0;
	GpioCtrlRegs.GPBDIR.bit.GPIO54 = 0;
	GpioCtrlRegs.GPBPUD.bit.GPIO54 = 0;

	GpioCtrlRegs.GPBMUX2.bit.GPIO55 = 0;
	GpioCtrlRegs.GPBDIR.bit.GPIO55 = 0;
	GpioCtrlRegs.GPBPUD.bit.GPIO55 = 0;

	GpioCtrlRegs.GPBMUX2.bit.GPIO56 = 0;
	GpioCtrlRegs.GPBDIR.bit.GPIO56 = 0;
	GpioCtrlRegs.GPBPUD.bit.GPIO56 = 0;

	GpioCtrlRegs.GPBMUX2.bit.GPIO48 = 0;
	GpioCtrlRegs.GPBDIR.bit.GPIO48 = 1;
	GpioCtrlRegs.GPBPUD.bit.GPIO48 = 0;

	GpioCtrlRegs.GPAMUX2.bit.GPIO26 = 0;
	GpioCtrlRegs.GPADIR.bit.GPIO26 = 1;
	GpioCtrlRegs.GPAPUD.bit.GPIO26 = 0;

	GpioCtrlRegs.GPAMUX2.bit.GPIO27 = 0;
	GpioCtrlRegs.GPADIR.bit.GPIO27 = 1;
	GpioCtrlRegs.GPAPUD.bit.GPIO27 = 0;

	EDIS;
}

Uint8 Read_X_GPIO(Uint8 Id){//Id 0:J8 1 J9 2 J11
	if(Id == 0x00){
		J8_EN;
		if(XGPIO_PRESS == U2_89 || XGPIO_PRESS == U2_90 || XGPIO_PRESS == U2_91 || XGPIO_PRESS == U2_94 || XGPIO_PRESS == U2_95 || XGPIO_PRESS == U2_96 || XGPIO_PRESS == U2_97 || XGPIO_PRESS == U2_98 ){
			if(XGPIO_PRESS == U2_89)return J8_0_Press;
			if(XGPIO_PRESS == U2_90)return J8_1_Press;
			if(XGPIO_PRESS == U2_91)return J8_2_Press;
			if(XGPIO_PRESS == U2_94)return J8_3_Press;
			if(XGPIO_PRESS == U2_95)return J8_4_Press;
			if(XGPIO_PRESS == U2_96)return J8_5_Press;
			if(XGPIO_PRESS == U2_97)return J8_6_Press;
			if(XGPIO_PRESS == U2_98)return J8_7_Press;
		}
	}
	if(Id == 0x01){
		J9_EN;
		if(XGPIO_PRESS == U2_89 || XGPIO_PRESS == U2_90 || XGPIO_PRESS == U2_91 || XGPIO_PRESS == U2_94 || XGPIO_PRESS == U2_95 || XGPIO_PRESS == U2_96 || XGPIO_PRESS == U2_97 || XGPIO_PRESS == U2_98 ){
			if(XGPIO_PRESS == U2_89)return J9_0_Press;
			if(XGPIO_PRESS == U2_90)return J9_1_Press;
			if(XGPIO_PRESS == U2_91)return J9_2_Press;
			if(XGPIO_PRESS == U2_94)return J9_3_Press;
			if(XGPIO_PRESS == U2_95)return J9_4_Press;
			if(XGPIO_PRESS == U2_96)return J9_5_Press;
			if(XGPIO_PRESS == U2_97)return J9_6_Press;
			if(XGPIO_PRESS == U2_98)return J9_7_Press;
		}
	}
	if(Id == 0x02){
		J11_EN;
		if(XGPIO_PRESS == U2_89 || XGPIO_PRESS == U2_90 || XGPIO_PRESS == U2_91 || XGPIO_PRESS == U2_94 || XGPIO_PRESS == U2_95 || XGPIO_PRESS == U2_96 || XGPIO_PRESS == U2_97 || XGPIO_PRESS == U2_98 ){
			if(XGPIO_PRESS == U2_89)return J11_0_Press;
			if(XGPIO_PRESS == U2_90)return J11_1_Press;
			if(XGPIO_PRESS == U2_91)return J11_2_Press;
			if(XGPIO_PRESS == U2_94)return J11_3_Press;
			if(XGPIO_PRESS == U2_95)return J11_4_Press;
			if(XGPIO_PRESS == U2_96)return J11_5_Press;
			if(XGPIO_PRESS == U2_97)return J11_6_Press;
			if(XGPIO_PRESS == U2_98)return J11_7_Press;
		}
	}
	return 0xff;
}

void Delay(void){
    Uint32 j;
    Uint16 i;
    for(i = 0;i<32;i++)
        for(j = 0;j<100000;j++);
}


int main(void){
	InitSysCtrl();
	LED_Config();
	Y_GPIO_Config();
	O_GPIO_Config();
	XM_GPIO_Config();
	X_GPIO_Config();

	while(1){
		LED_ON;
		Delay();
		LED_OFF;
		Delay();
	}

}


